package com.share.user.api.factory;


import com.share.common.core.domain.R;
import com.share.user.api.RemoteUserInfoService;
import com.share.user.api.domain.UserInfo;
import com.share.user.api.modei.UpdateUserLogin;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class RemoteUserInfoFallbackFactory implements FallbackFactory<RemoteUserInfoService> {
    @Override
    public RemoteUserInfoService create(Throwable cause) {
        return new RemoteUserInfoService() {

            @Override
            public R<UserInfo> wxLogin(String code) {
                log.error("【远程服务熔断】微信登录服务调用失败，code={}, 原因: {}", code, cause.getMessage(), cause);
                return R.fail("微信登录服务暂时不可用，请稍后再试");
            }

            @Override
            public R<Boolean> updateUserLogin(UpdateUserLogin updateUserLogin) {
                String userId = updateUserLogin != null ? String.valueOf(updateUserLogin.getUserId()) : "null";
                log.error("【远程服务熔断】更新用户登录信息失败，userId={}, 原因: {}", userId, cause.getMessage(), cause);
                return R.fail("用户登录信息更新服务暂时不可用");
            }

            @Override
            public R<UserInfo> getUserInfo(Long userId) {
                log.error("【远程服务熔断】获取用户信息失败，userId={}, 原因: {}", userId, cause.getMessage(), cause);
                return R.fail("用户信息获取服务暂时不可用");
            }
        };
    }
}
